package com.example.bilibili;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.example.bilibili.home.FragmentA;
import com.example.bilibili.home.FragmentB;
import com.example.bilibili.home.GuodanianFragment;
import com.example.bilibili.home.KangjifeiyanFragment;
import com.example.bilibili.home.RemenFragment;
import com.example.bilibili.home.TuijianFragment;
import com.example.bilibili.home.TuijianFragmentAdapter;
import com.example.bilibili.home.XiaokangFragment;
import com.example.bilibili.home.YingshiFragment;
import com.example.bilibili.home.ZhiboFragment;
import com.example.bilibili.home.ZhuifanFragment;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.GridLayoutManager;
import okhttp3.Call;

public class HomeFragment extends Fragment {

    protected Context mContext;

    private int position = 1;
    private ArrayList<Fragment> fragments;
    private Fragment tempFragment;

    private EditText tv_search_home;
    private ImageButton game;
    private ImageButton bainianji;
    private ImageButton message;
    private ImageButton sousuo;
    private FrameLayout frameLayout2;
    private LinearLayout line2;
    private String Sguanjianci;

    private static final String TAG = TuijianFragment.class.getSimpleName();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home,container,false);
        RadioGroup rg_main2 = view.findViewById(R.id.rg_main2);
        tv_search_home = (EditText) view.findViewById(R.id.tv_search_home);
        game = (ImageButton) view.findViewById(R.id.game);
        bainianji = (ImageButton) view.findViewById(R.id.bainianji);
        message = (ImageButton) view.findViewById(R.id.message);
        sousuo = (ImageButton) view.findViewById(R.id.sousuo);
        frameLayout2 = (FrameLayout) view.findViewById(R.id.frameLayout2);
        line2 = (LinearLayout) view.findViewById(R.id.line2);
        initFragment();
        rg_main2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_zhibo:
                        position = 0;
                        break;
                    case R.id.rb_remen:
                        position = 2;
                        break;
                    case R.id.rb_zhuifan:
                        position = 3;
                        break;
                    case R.id.rb_yingshi:
                        position = 4;
                        break;
                    case R.id.rb_guodanian:
                        position = 5;
                        break;
                    case R.id.rb_kangjifeiyan:
                        position = 6;
                        break;
                    case R.id.rb_xiaokang:
                        position = 7;
                        break;
                    default://推荐
                        position = 1;
                        break;
                }
                //获取当前的fragment
                Fragment baseFragment = getFragment(position);
                //实现fragment的跳转
                switchFragment(tempFragment,baseFragment);
            }
        });


        //搜索的监听
        sousuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, SousuoActivity.class);
                Sguanjianci = tv_search_home.getText().toString();
                intent.putExtra("Sguanjianci", Sguanjianci);
                mContext.startActivity(intent);
            }
        });


        //消息的监听
        message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "消息", Toast.LENGTH_SHORT).show();
            }
        });

        //游戏的监听
        game.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "游戏", Toast.LENGTH_SHORT).show();
            }
        });

        //拜年祭的监听
        bainianji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "拜年祭", Toast.LENGTH_SHORT).show();
            }
        });
        rg_main2.check(R.id.rb_tuijian);
        return view;
    }


    private void switchFragment(Fragment fromFragment, Fragment nextFragment) {
        if (tempFragment != nextFragment) {
            tempFragment = nextFragment;
            if (nextFragment != null) {
                FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                //判断nextFragment是否添加
                if (!nextFragment.isAdded()) {
                    //隐藏当前Fragment
                    if (fromFragment != null) {
                        transaction.hide(fromFragment);
                    }
                    //添加Fragment
                    transaction.add(R.id.frameLayout2, nextFragment).commit();
                } else {
                    //隐藏当前Fragment
                    if (fromFragment != null) {
                        transaction.hide(fromFragment);
                    }
                    transaction.show(nextFragment).commit();
                }
            }
        }
    }



    private Fragment getFragment(int position) {
        if(fragments!=null && fragments.size()>0){
            return fragments.get(position);
        }
        return null;
    }

    private void initFragment() {
        fragments = new ArrayList<>();
        fragments.add(new ZhiboFragment());
        fragments.add(new TuijianFragment());
        fragments.add(new RemenFragment());
        fragments.add(new ZhuifanFragment());
        fragments.add(new YingshiFragment());
        fragments.add(new GuodanianFragment());
        fragments.add(new KangjifeiyanFragment());
        fragments.add(new XiaokangFragment());
    }


}

